package tablero;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Tablero.java
 *
 * Created on 20-oct-2012, 9:33:32
 */
/**
 *
 * @author Joshua
 */

import com.codethesis.pgnparse.MalformedMoveException;
import com.codethesis.pgnparse.PGNParseException;
import com.supareno.pgnparser.PGNWriter;
import java.io.IOException;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.apache.log4j.*;


public class Tablero extends javax.swing.JFrame {


    private int NumeroColumna = 0;
    private int NumeroFila = 0;
    private int _Tamaño = 8;
    public String _Accion= "b";
    private int X =0;
    private int Y=0;
    public String Fichas[][] = {{"tn","an","cn","dn","rn","cn","an","tn"},
        {"pn","pn","pn","pn","pn","pn","pn","pn"},
        {"","","","","","","",""},
        {"","","","","","","",""},
        {"","","","","","","",""},
        {"","","","","","","",""},
        {"pb","pb","pb","pb","pb","pb","pb","pb"},
        {"tb","ab","cb","db","rb","cb","ab","tb"}}; 
    public Ficha Tablero[][] = new Ficha[8][8];
    private int JuegoAnterior;
    public Boolean _Hilo = false;
    private ManejoPGN manejoPGN;
    
    /** Creates new form Tablero */
    public Tablero() {
        initComponents();
        CrearTablero();
         manejoPGN= new ManejoPGN();
    }
    
    private void CrearTablero()
    {
        for(int i =0;i < 8; i++){
            for(int j =0; j < 8; j++){
                if(Fichas[j][i].equals("")){
                    Tablero[j][i]=null;
                    AgregarImagen("Imagenes/",i,j);
                }
                else{
                    Tablero[j][i]=new Ficha(Fichas[j][i]);
                    AgregarImagen("Imagenes/" +Tablero[j][i].tipo+Tablero[j][i].color,i,j);
                }
            }
        }
    }
    
    public void AgregarImagen(String nombre,int i, int j){
        JLabel Imagen = new JLabel();
        if((i + j) % 2 == 0)
        {
            nombre +="b.png";
        }
        else{
            nombre +="n.png";
        }
        Imagen.setIcon(new ImageIcon(getClass().getClassLoader().getResource(nombre)));
        if(jPanel1.getComponentAt(i*70,j*70)!=null){
            jPanel1.remove(jPanel1.getComponentAt(i*70,j*70));
        }
        Imagen.setBounds(i*70,j*70,70,70);
        jPanel1.add(Imagen);
        /*String salida="";
        for(int k =0;k < 8; k++){
            for(int l =0; l < 8; l++){
                if (Tablero[k][l]==""){
                    salida += "   ";
                }
                else{
                    salida += Tablero[k][l] + " ";
                }
            }
            salida+="\n";
        }
        System.out.println(salida*/
        repaint();
    }

    private void AgregarEspacio(String nombre,int i, int j){
        JLabel Imagen1 = new JLabel();
        Imagen1.setIcon(new ImageIcon(getClass().getClassLoader().getResource(nombre)));
        if(jPanel1.getComponentAt(i*50+50,j*50+50)!=null){
           jPanel1.remove(jPanel1.getComponentAt(i*50+50,j*50+50));
        }
        Imagen1.setBounds(i*50+50,j*50+50,50,50);
        jPanel1.add(Imagen1);
        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Chess-DB");

        jPanel1.setMaximumSize(new java.awt.Dimension(560, 580));
        jPanel1.setMinimumSize(new java.awt.Dimension(560, 580));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 560, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 622, Short.MAX_VALUE)
        );

        jLabel1.setText("Etiquetar: ");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(138, 138, 138)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(388, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(72, 72, 72))
        );

        jMenuBar2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jMenu2.setText("Archivo");

        jMenuItem7.setText("Salir");
        jMenu2.add(jMenuItem7);

        jMenuBar2.add(jMenu2);

        jMenu3.setText("Partida");

        jMenuItem8.setText("Nueva Partida");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem8);

        jMenuItem1.setText("Cargar Partidas");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuItem9.setText("Mostrar Estadísticas");
        jMenu3.add(jMenuItem9);

        jMenuItem2.setText("Guardar Partidas");
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("Borrar Partida");
        jMenu3.add(jMenuItem3);

        jMenuBar2.add(jMenu3);

        jMenu4.setText("Tablero");

        jMenuItem4.setText("Conectar tablero físico");
        jMenu4.add(jMenuItem4);

        jMenuItem5.setText("Desconectar tablero físico");
        jMenu4.add(jMenuItem5);

        jMenuBar2.add(jMenu4);

        jMenu1.setText("Ayuda");

        jMenuItem6.setText("Mostrar Ayuda");
        jMenu1.add(jMenuItem6);

        jMenuBar2.add(jMenu1);

        setJMenuBar(jMenuBar2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(12, 173, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        JFileChooser filechooser = new JFileChooser();
        int result = filechooser.showOpenDialog(null);
        if(result == JFileChooser.APPROVE_OPTION){
            try {
                manejoPGN.cargarPGN(filechooser.getSelectedFile());
                JOptionPane.showMessageDialog(this, "Las partidas se han cargado exitosamente");
                
            } catch (IOException ex) {
               
            } catch (PGNParseException ex) {
                
            } catch (NullPointerException ex) {
                
            } catch (MalformedMoveException ex) {
                
            }
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Tablero Tablero1 = new Tablero();
                Tablero1.setVisible(true);
                Coordenadas ObtenerCoordenadas = new Coordenadas( Tablero1 );
                Tablero1.addMouseListener( ObtenerCoordenadas );

                 

            }
        });
        
           /*     Partida a= new Partida();
                List<Game> b=a.buscarPartida("Lloyds Bank op", "London", "", "", "", "D42");
                 PGNWriter writer =new PGNWriter();
                 writer.setFileName("archivogenerado");
                 writer.setLoggerLevel(Level.ALL);
                 writer.writePGNGame(b.get(0));*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
